package com.imay.photopicker.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetworkUtil {
    private static final String TAG = NetworkUtil.class.getSimpleName();
    public static final int NO_NETWORK = 0;
    public static final int WIFI_NETWORK = 1;
    public static final int G_NETWORK = 2;
    public static final int LINE_NETWORK = 3;

    private static final int LOW_SPEED_UPLOAD_BUF_SIZE = 1024;
    private static final int HIGH_SPEED_UPLOAD_BUF_SIZE = 10240;
    private static final int MAX_SPEED_UPLOAD_BUF_SIZE = 102400;
    private static final int LOW_SPEED_DOWNLOAD_BUF_SIZE = 2024;
    private static final int HIGH_SPEED_DOWNLOAD_BUF_SIZE = 30720;
    private static final int MAX_SPEED_DOWNLOAD_BUF_SIZE = 102400;

    private static final int NETWORK_TYPE_IDEN = 11; // Level 8
    private static final int NETWORK_TYPE_EVDO_B = 12; // Level 9
    private static final int NETWORK_TYPE_LTE = 13; // Level 11
    private static final int NETWORK_TYPE_EHRPD = 14; // Level 11
    private static final int NETWORK_TYPE_HSPAP = 15; // Level 13

    public static boolean hasNetwork(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isAvailable();
            }
            return false;
        }
        return false;
    }

    /***
     * 1 ：wifi、2:移动网络 、3：有线网络 0、无网络
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                //Wifi
                return WIFI_NETWORK;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                //3G
                return G_NETWORK;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                //有线
                return LINE_NETWORK;
            }
        }
        return NO_NETWORK;
    }

    public static boolean isWifiConnection(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if ((networkInfo != null) && (networkInfo.isConnected())) {
            return true;
        }
        return false;
    }

    public static int getUploadBufSize(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if ((networkInfo != null) && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
            return MAX_SPEED_UPLOAD_BUF_SIZE;
        }
        if ((networkInfo != null)  && (isConnectionFast(networkInfo.getType(), networkInfo.getSubtype()))) {
            return HIGH_SPEED_UPLOAD_BUF_SIZE;
        }
        return LOW_SPEED_UPLOAD_BUF_SIZE;
    }

    public static int getDownloadBufSize(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if ((networkInfo != null) && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
            return MAX_SPEED_DOWNLOAD_BUF_SIZE;
        }
        if ((networkInfo != null) && (isConnectionFast(networkInfo.getType(), networkInfo.getSubtype()))) {
            return HIGH_SPEED_DOWNLOAD_BUF_SIZE;
        }
        return LOW_SPEED_DOWNLOAD_BUF_SIZE;
    }

    private static boolean isConnectionFast(int type, int subType) {
        if (type == ConnectivityManager.TYPE_WIFI) {
            return true;
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            switch (subType) {
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                    return false; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_CDMA:
                    return false; // ~ 14-64 kbps
                case TelephonyManager.NETWORK_TYPE_EDGE:
                    return false; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    return true; // ~ 400-1000 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    return true; // ~ 600-1400 kbps
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    return false; // ~ 100 kbps
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                    return true; // ~ 2-14 Mbps
                case TelephonyManager.NETWORK_TYPE_HSPA:
                    return true; // ~ 700-1700 kbps
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                    return true; // ~ 1-23 Mbps
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    return true; // ~ 400-7000 kbps
                // NOT AVAILABLE YET IN API LEVEL 7
                case NETWORK_TYPE_EHRPD:
                    return true; // ~ 1-2 Mbps
                case NETWORK_TYPE_EVDO_B:
                    return true; // ~ 5 Mbps
                case NETWORK_TYPE_HSPAP:
                    return true; // ~ 10-20 Mbps
                case NETWORK_TYPE_IDEN:
                    return false; // ~25 kbps
                case NETWORK_TYPE_LTE:
                    return true; // ~ 10+ Mbps
                // Unknown
                case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                    return false;
                default:
                    return false;
            }
        } else {
            return false;
        }
    }
}
